package com.ruoyi.toolbox.service.impl;

import com.ruoyi.common.utils.file.FileSizeUtils;
import com.ruoyi.toolbox.domain.database.Table;
import com.ruoyi.toolbox.mapper.DatabaseMapper;
import com.ruoyi.toolbox.service.IDatabaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Objects;

/**
 * 数据库 服务实现类
 *
 * @author tqfeiyang
 * @since 2024/11/26 11:54
 */
@Component
@RequiredArgsConstructor
public class DatabaseServiceImpl implements IDatabaseService
{
    private final DatabaseMapper databaseMapper;

    /**
     * 查询数据表
     *
     * @return 数据表列表
     */
    @Override
    public List<Table> selectTableList()
    {
        return databaseMapper.selectTableList().stream()
                .map((table) -> {
                    if (Objects.nonNull(table.getDataLength()))
                    {
                        table.setDataSize(FileSizeUtils.formatSize(table.getDataLength()));
                    }
                    return table;
                }).toList();
    }

    /**
     * 优化数据表
     *
     * @param list 数据表
     */
    @Override
    public void optimizeTableList(List<String> list)
    {
        Assert.notNull(list, "list不能为null");
        Assert.noNullElements(list, "list中不能包含null");
        databaseMapper.optimizeTableList(list);

    }

    /**
     * 修复据表
     *
     * @param list 数据表
     */
    @Override
    public void repairTableList(List<String> list)
    {
        Assert.notNull(list, "list不能为null");
        Assert.noNullElements(list, "list中不能包含null");
        databaseMapper.repairTableList(list);
    }
}
